﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Product;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.ManageProduct
{
    public partial class ProductAdd : UserControl
    {
        private static ProductLogic productBll;
        public T_Product product;
        private T_ProductExt productExt;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public ProductAdd()
        {
            productBll = new ProductLogic();
            product = new T_Product();
            productExt = new T_ProductExt();
            InitializeComponent();
            LoadControlData();
        }
        private void ProductAdd_Load(object sender, EventArgs e)
        {
            txtProdNo.Focus();
            txtProdNo.Select();
        }
        /// <summary>
        /// 初始化控件数据
        /// </summary>
        private void LoadControlData()
        {
            ComboLogic comboLogic = new ComboLogic();
            cmbBrokerageType.DisplayMember = "DisplayText";
            cmbBrokerageType.ValueMember = "Value1";
            cmbBrokerageType.DataSource = comboLogic.GetComboByBuzType("BrokerageType");

            cmbUnit.DisplayMember = "DisplayText";
            cmbUnit.ValueMember = "Value1";
            cmbUnit.DataSource = comboLogic.GetComboByBuzType("Unit");

            cmbSubUnit.DisplayMember = "DisplayText";
            cmbSubUnit.ValueMember = "Value1";
            cmbSubUnit.DataSource = comboLogic.GetComboByBuzType("Unit");

            cmbProvider.DisplayMember = "Name";
            cmbProvider.ValueMember = "Id";
            cmbProvider.DataSource = productBll.GetProvider();

            txtProdNo.Text = productBll.GetCountNo("T_Product", false);
        }

        public void SetTreeValue(string treeId, string treeName)
        {
            product.TreeId = treeId;
            txtProductType.Text = treeName;
        }

        public void GetProduct(string id)
        {
            product.Id = id;

            product = productBll.Where<T_Product>(p => p.Id == id).FirstOrDefault();
            productExt = productBll.Where<T_ProductExt>(p => p.ProductId == id).FirstOrDefault();

            txtName.Text = product.Name;
            txtModel.Text = product.Model;
            txtProdNo.Text = product.ProductNo;
            txtProductNum.Text = product.ProductNum;
            txtShortName.Text = product.ShortName;
            txtSpecification.Text = product.Specification;
            txtProductType.Text = productBll.Where<T_Tree>(t => t.Id == product.TreeId).FirstOrDefault().Name;
            cmbUnit.Text = product.Unit;

            txtBarCode.Text = productExt.BarCode;
            numInPrice.Value = productExt.InPrice;
            numSalePrice.Value = productExt.SalePrice;
            numAlertStore.Value = productExt.AlertStore;
            numScore.Value = productExt.ChargeMark;
            numBrokerageNum.Value = productExt.BrokerageNum;
            cmbBrokerageType.Text = productExt.BrokerageType;
            cmbProvider.SelectedValue = productBll.Where<T_Provider>(p => p.Id == productExt.ProviderId).FirstOrDefault().Id;
            cmbSubUnit.Text = productExt.SubUnit;
            numSubAmount.Value = productExt.SubAmount;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                SetValue();
                ProductInput.Instance.GenerateGridView();
                if (string.IsNullOrEmpty(product.Id))
                {
                    product.Id = Guid.NewGuid().ToString("D");
                    productExt.ExtId = Guid.NewGuid().ToString("D");
                    productExt.ProductId = product.Id;
                    productBll.AddProduct(product, productExt);
                    //FormSysMessage.ShowSuccessMsg("添加成功！");
                }
                else
                {
                    productBll.UpdateProduct(product, productExt);
                    //FormSysMessage.ShowSuccessMsg("修改成功！");
                }
                this.FindForm().Close();
                ProductList.Instance.BtnSearch_Click(null, null);
            }
        }

        private void SetValue()
        {
            product.Name = txtName.Text.Trim();
            product.ProductNo = txtProdNo.Text.Trim();
            product.ProductNum = txtProductNum.Text.Trim();
            product.Model = txtModel.Text.Trim();
            product.ShortName = txtShortName.Text.Trim();
            product.Specification = txtSpecification.Text.Trim();
            product.Unit = cmbUnit.Text.Trim();
            product.AddTime = DateTime.Now;
            product.IsProject = "否";


            productExt.AlertStore = numAlertStore.Value;
            productExt.BarCode = txtBarCode.Text.Trim();
            productExt.BrokerageNum = numBrokerageNum.Value;
            productExt.BrokerageType = cmbBrokerageType.Text.Trim();
            productExt.ChargeMark = numScore.Value;
            productExt.InPrice = numInPrice.Value;
            productExt.SalePrice = numSalePrice.Value;
            if (cmbProvider.SelectedValue != null)
            {
                productExt.ProviderId = cmbProvider.SelectedValue.ToString();
            }
            productExt.SubUnit = cmbSubUnit.Text; ;
            productExt.SubAmount = numSubAmount.Value;
            //ProductInput.Instance.productView = new Orm.View.V_Product();
            //ProductInput.Instance.productView.Name = txtName.Text;
            //ProductInput.Instance.productView.InPrice = numInPrice.Value;
            //ProductInput.Instance.productView.Unit = cmbUnit.Text;

        }

        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtProdNo.Text.Trim()))
            {
                FormSysMessage.ShowSuccessMsg("请输入编号！");
                return false;
            }
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                FormSysMessage.ShowSuccessMsg("请输入名称！");
                return false;
            }
            if (string.IsNullOrEmpty(txtProductType.Text.Trim()))
            {
                FormSysMessage.ShowSuccessMsg("请选择产品类型！");
                return false;
            }

            if (string.IsNullOrEmpty(cmbProvider.Text.Trim()))
            {
                FormSysMessage.ShowSuccessMsg("请选择供应商！");
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void btnProvider_Click(object sender, EventArgs e)
        {

        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void pbClose_Click_1(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void btnType_Click_1(object sender, EventArgs e)
        {
            FormTreeSelecter formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("ProductTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;

            product.TreeId = formTreeSelecter.TypeId;
            txtProductType.Text = formTreeSelecter.TypeText;
        }

        private void cmbUnit_TextChanged(object sender, EventArgs e)
        {
            txtSpecification.Text = string.Format("{0}{1}/{2}", numSubAmount.Value, cmbSubUnit.Text.Trim(), cmbUnit.Text.Trim());
        }

        private void cmbSubUnit_TextChanged(object sender, EventArgs e)
        {
            txtSpecification.Text = string.Format("{0}{1}/{2}", numSubAmount.Value, cmbSubUnit.Text.Trim(), cmbUnit.Text.Trim());
        }

        private void cmbUnit_KeyUp(object sender, KeyEventArgs e)
        {
            SPACommon.SelectNextControl(sender as Control, e);
        }

        private void cmbSubUnit_KeyUp(object sender, KeyEventArgs e)
        {
            SPACommon.SelectNextControl(sender as Control, e);
        }

        private void numSubAmount_KeyUp(object sender, KeyEventArgs e)
        {
            txtSpecification.Text = string.Format("{0}{1}/{2}", numSubAmount.Value, cmbSubUnit.Text.Trim(), cmbUnit.Text.Trim());
        }

        private void numSubAmount_ValueChanged(object sender, EventArgs e)
        {
            txtSpecification.Text = string.Format("{0}{1}/{2}", numSubAmount.Value, cmbSubUnit.Text.Trim(), cmbUnit.Text.Trim());
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }
    }
}
